<!DOCTYPE html>
<html lang="en">
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <title>智能组卷系统</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div id="userController" class="app-container">
        <div class="box">
            <!-- 筛选器 -->
            <div class="filter-container" style="padding:10px 0 15px 800px;">
                <el-input placeholder="用户名/电话/姓名" v-model="pagination.selection" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="getUser()" type="primary" class="dalfBut">查询</el-button>
                <el-button @click="resetSelection() " class="dalfBut">重置</el-button>
                <el-button @click="handleCreate()" type="primary" class="butT">新增用户</el-button>
            </div>

            <!-- 数据表格 -->
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="username" label="用户名" align="center"></el-table-column>
                <el-table-column prop="name" label="姓名" align="center"></el-table-column>
                <el-table-column prop="telephone" label="电话号码" align="center"></el-table-column>

                <el-table-column prop="status" label="身份" align="center">
                    <template slot-scope="scope">
                        {{ String(scope.row.status) == '0' ? '用户(封禁)' : String(scope.row.status) == '2' ?
                        '管理员':'用户(正常)'}}
                    </template>
                </el-table-column>

                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button v-if="scope.row.status===0" plain type="text" size="mini" @click="unban(scope.row)">
                            解禁
                        </el-button>
                        <el-button v-if="scope.row.status===1" plain type="text" size="mini" @click="ban(scope.row)"
                                   style="color: red;">禁用
                        </el-button>
                        <el-button v-if="scope.row.status!=2" plain type="text" size="mini"
                                   @click="handleUpdate(scope.row)" style="color: forestgreen;">
                            编辑
                        </el-button>
                    </template>
                </el-table-column>
                <!--                <el-divider></el-divider>-->
            </el-table>

            <!-- 分页组件 -->
            <div class="pagination-container">
                <el-pagination
                        class="pagination"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total,prev,pager,next,jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>

            <!-- 新增弹窗 -->
            <div class="add-form">
                <el-dialog title="新增用户" :visible.sync="addFormVisible" width="660px">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="用户类型" prop="status" label-width="100px">
                                    <el-select v-model="formData.status" clearable>
                                        <el-option label="用户" value=1 ></el-option>
                                        <el-option label="管理员" value=2 ></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="用户名" prop="username" label-width="100px">
                                    <el-input v-model="formData.username"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="密码" prop="password" label-width="100px">
                                    <el-input type="password" v-model="formData.password"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>

                            <el-col :span="12">
                                <el-form-item label="姓名" prop="name" label-width="100px">
                                    <el-input v-model="formData.name" placeholder="姓名"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="电话" prop="telephone" label-width="100px">
                                    <el-input v-model="formData.telephone" placeholder="电话"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>

                    <div slot="footer" class="dialog-footer">
                        <el-button @click="addFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑弹窗 -->
            <div class="add-form">
                <el-dialog title="编辑用户" :visible.sync="editFormVisible">
                    <el-form ref="dataAddForm" :model="formData" label-position="right">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="用户名:" prop="username" label-width="100px" >
                                    {{formData.username}}
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="姓名" prop="name" label-width="100px">
                                    <el-input v-model="formData.name" placeholder="姓名"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="电话" prop="telephone" label-width="100px">
                                    <el-input v-model="formData.telephone" placeholder="电话"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>

                    <div slot="footer" class="dialog-footer">
                        <el-button @click="editFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>


</div>

</body>
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios.min.js"></script>
<!--<script src="../js/md5.js"></script>-->
<script>
    var vue = new Vue({

        el: '#app',
        data: {
            page: 1,
            pagination: {
                currentPage: 1,  //当前页码
                pageSize: 10,    //每页记录
                total: 0,        //总记录
                selection: "",   //查找框
            },//分页数据

            dataList: [],//当前页要展示的列表数据
            formData: {},//表单数据

            addFormVisible: false,          //控制表单是否可见
            editFormVisible: false,         //编辑表单是否可见
            rules: {//校验规则
                username: [{required: true, message: '用户名不可为空', trigger: 'blur'},
                    {max: 20, message: '输入字段过长', trigger: 'blur'}],
                name: [{max: 20, message: '输入字段过长', trigger: 'blur'}],
                telephone: [{max: 20, message: '输入字段过长', trigger: 'blur'}],
            },
        },

        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            const userInfo = window.localStorage.getItem('userInfo')
            if (userInfo) {
                this.userInfo = JSON.parse(userInfo)
            }
            this.getUser();
        },

        methods: {

            //显示用户信息
            getUser() {
                param = "?username=" + this.pagination.selection;
                param += "&name=" + this.pagination.selection;
                param += "&telephone=" + this.pagination.selection;
                axios.get("/user/" + this.pagination.currentPage + "/" + this.pagination.pageSize + param).then((res) => {
                    this.pagination.pageSize = res.data.data.size;
                    this.pagination.currentPage = res.data.data.current;
                    this.pagination.total = res.data.data.total;
                    this.dataList = res.data.data.records;
                });
            },

            //分页
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.getUser()
            },

            //弹出添加窗口
            handleCreate() {
                this.addFormVisible = true;
                this.resetForm();
            },

            //重置表单
            resetForm() {
                this.formData = {};
            },

            //添加用户
            handleAdd() {
                axios.post("/user", this.formData).then((res) => {
                    if (res.data.code === 20010) {
                        this.addFormVisible = false;
                        this.$message.success("添加成功");
                    } else if (res.data.code === 20011) {
                        this.$message.error("添加失败,"+res.data.msg);
                    } else {
                        this.$message.error(res.data.msg);
                    }
                }).finally(() => {
                    this.getUser();
                });
            },

            //弹出修改窗口
            handleUpdate(row) {
                axios.get("/user/" + row.id).then((res) => {
                    if (res.data.code === 20040) {
                        this.formData = res.data.data;
                        this.editFormVisible = true;
                    } else {
                        this.$message.error(res.data.msg);
                    }
                });
            },

            //修改用户
            handleEdit() {
                axios.put("/user", this.formData).then((res) => {
                    if (res.data.code === 20030) {
                        this.editFormVisible = false;
                        this.$message.success("修改成功");
                    } else if (res.data.code === 20031) {
                        this.$message.error("修改失败");
                    } else {
                        this.$message.error(res.data.msg);
                    }
                }).finally(() => {
                    this.getUser();
                });
            },

            //禁用用户
            ban(row) {
                this.$confirm("确定禁用该用户？", "提示", {
                    type: 'info'
                }).then(() => {
                    row.status = 0;
                    axios.put("/user", row).then((res) => {
                        if (res.data.code === 20030) {
                            this.$message.success("已禁用该用户");
                        } else {
                            this.$message.error("禁用失败");
                        }
                    }).finally(() => {
                        this.getUser();
                    });
                }).catch(() => {
                    this.$message.info("取消禁用");
                });

            },
            
            //解禁用户
            unban(row) {
                this.$confirm("确定解禁该用户？", "提示", {
                    type: 'info'
                }).then(() => {
                    row.status = 1;
                    console.log(row)
                    axios.put("/user", row).then((res) => {
                        console.log(res.data.code)
                        if (res.data.code === 20030) {
                            this.$message.success("已解禁该用户");
                        } else {
                            this.$message.error("解禁失败");
                        }
                    }).finally(() => {
                        this.getUser();
                    });

                }).catch(() => {
                    this.$message.info("取消解禁");
                });
                
            },

            //重置搜索框
            resetSelection() {
                this.pagination.selection = "";
                this.getUser();
            },

        }
    })
</script>
</html>